package com.library.manager.service;

import com.library.manager.dao.ManagerDao;
import com.library.manager.dao.ManagerDaoImpl;
import com.library.manager.model.Manager;
import com.library.util.MD5Util;

import java.util.Date;

public class ManagerServiceImpl implements ManagerService {
    private ManagerDao managerDao = new ManagerDaoImpl();

    @Override
    public Manager login(String username, String password) {
        // 对密码进行MD5加密
        String encryptedPassword = MD5Util.encrypt(password);
        return managerDao.findByUsernameAndPassword(username, encryptedPassword);
    }

    @Override
    public boolean register(Manager manager) {
        // 检查用户名是否已存在
        if (managerDao.findByUsername(manager.getManagerusername()) != null) {
            return false;
        }
        
        // 对密码进行MD5加密
        String encryptedPassword = MD5Util.encrypt(manager.getManagerpassword());
        manager.setManagerpassword(encryptedPassword);
        
        // 设置创建时间
        manager.setCreatetime(new Date());
        
        // 添加管理员
        return managerDao.addManager(manager) > 0;
    }

    @Override
    public boolean updateManager(Manager manager) {
        return managerDao.updateManager(manager) > 0;
    }

    @Override
    public boolean deleteManager(Integer managerid) {
        return managerDao.deleteManager(managerid) > 0;
    }
} 